package com.example.fufudamowang.androidservice;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.view.View;
import android.widget.Button;


import java.util.Calendar;

public abstract class MainActivity extends Activity implements View.OnClickListener {
    private Button B1,B2;
    boolean k=false;
    private static MainActivity appRef = null;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        appRef=this;
        setContentView(R.layout.activity_main);
        B1=(Button) findViewById(R.id.B1);
        B1.setOnClickListener(this);
        B2=(Button)findViewById(R.id.B2);
        B2.setOnClickListener(this);
    }

    public static MainActivity getApp() {
        return appRef;
    }

    public void btEvent(String data) {
        setTitle(data);
    }

    public void onClick(View arg0) {
        if (arg0 == B1) {
            setTitle("Waiting... Alarm=5");
            Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
            PendingIntent p_intent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 3);

            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),p_intent);
        }

        if (arg0 == B2) {

            Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
            PendingIntent p_intent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);


            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.cancel(p_intent);
            finish();

        }
    }
}
